<?php

include_once "Models\Player.php";

class dbPlayer {
	
function newPlayer($firstName, $lastName, $age, $position){
	$newPlayer = new Player();
	
	$newPlayer->firstName = $firstName;
	$newPlayer->lastName = $lastName;
	$newPlayer->age = $age;
	$newPlayer->position = $position;
	
	$db = connect();
	$sth = $db->prepare("INSERT INTO player VALUES(null, ?)");
	
	if($sth === false) {
		printf("Failed to prepare the SQL statement: %s.<br />",
		htmlspecialchars($db->error));
		return false;
	}
	$result = $sth->bind_param("s", $name);
	$name = $player->getPlayerName();
	if($result == false) {
		printf("Failed to bind params: %s.<br />", htmlspecialchars($sth->error));
		return false;
	}
	$result = $sth->execute();
	if($result == false) {
		printf("Failed to execute prepared statement %s.<br />: ",
		htmlspecialchars($sth->error));
		return false;
	}
	$id = $db->insert_id;
	return $id;
}

/**
 * Retrieves all records from the Player table using an instance of mysqli.
 * @return $allPlayer associative array corresponding to rows in 
 * player table
 */
 static function getAllPlayers() {
  $db = connect( );
  $sth = $db->prepare("SELECT * FROM Players");
  if($sth === false) {
   echo "Failed to prepare the SQL statement: " . htmlspecialchars($db->error);
   return false;
  }
  $result = $sth->execute();
  if($result == false) {
   echo "Failed to execute: " . htmlspecialchars($sth->error);
   return false;
  }
  $sth->bind_result($playerID, $name);

  $allPlayers = array( );
  while($sth->fetch()){
   $allPlayers[] = array('playerID' => $playerID, 'name' =>$name);
  }
  return $allPlayers;
 }
 /**
  * Adds an instance of player to the Player table using an instance 
  * of mysqli.
  * @param Player object $ -contains user-entered  
  * name.
  * @return int $id -last auto_increment value in Player table
  */
 static function addPlayer($player){
  $db = connect( );
  $sth = $db->prepare("INSERT INTO player VALUES(null, ?)");
  if($sth === false) {
   printf("Failed to prepare the SQL statement: %s.<br />",
   htmlspecialchars($db->error));
   return false;
  }
  $result = $sth->bind_param("s", $name);
  $name = $player->getPlayerName();
  if($result == false) {
   printf("Failed to bind params: %s.<br />", htmlspecialchars($sth->error));
   return false;
  }
  $result = $sth->execute();
  if($result == false) {
   printf("Failed to execute prepared statement %s.<br />: ",
   htmlspecialchars($sth->error));
   return false;
  }
  $id = $db->insert_id;
  return $id;
  }
  /**
 * Retrieves all records from the Player table using an instance of mysqli.
 * @return $allPlayers associative array corresponding to rows in 
 * player table
 */
 static function getPlayerByID($playerID) {
  $db = connect( );
  $sth = $db->prepare("SELECT * FROM player where playerID = ?");
  if($sth === false) {
   echo "Failed to prepare the SQL statement: " . htmlspecialchars($db->error);
   return false;
  }
  $result = $sth->bind_param("i", $playerID);
      if($result == false) {
        echo "Failed to bind params: " . htmlspecialchars($sth->error);
        return false;
  }
  $result = $sth->execute();
  if($result == false) {
   echo "Failed to execute: " . htmlspecialchars($sth->error);
   return false;
  }
  $sth->bind_result($playerID, $name);
  $allPlayers = array();
  while($sth->fetch()){
   $allPlayers = array('playerID' => $playerID, 'name' =>$name);
  }
  return $allPlayers;
 }
 
 /**
  * 
  * Deletes a player from the system
  * @param player_ID $id
  */
    static function deletePlayer($id) {
      $db = connect();
      $sth = $db->prepare("DELETE FROM player WHERE playerID=?");
      if($sth === false) {
        echo "Failed to prepare the SQL statement: " . htmlspecialchars($db->error);
        return false;
      }
      $result = $sth->bind_param('i',$id);
      if($result === false) {
        echo "Failed to bind params: " . htmlspecialchars($sth->error);
        return false;
      }
      $result = $sth->execute();
      if($result === false) {
        echo "Failed to execute: " . htmlspecialchars($sth->error);
        return false;
      }
      return true;
    }
    
static function updatePlayer($name, $id) {
      $db = connect();
      $sth = $db->prepare("UPDATE player SET name=? WHERE playerID = ?");
      if($sth === false) {
        echo "Failed to prepare the SQL statement: " . htmlspecialchars($db->error);
        return false;
      }
      $result = $sth->bind_param('si',$name, $id);
      if($result === false) {
        echo "Failed to bind params: " . htmlspecialchars($sth->error);
        return false;
      }
      $result = $sth->execute();
      if($result === false) {
        echo "Failed to execute: " . htmlspecialchars($sth->error);
        return false;
      }
      return true;
    }
}

?>